﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace Mrsoft.Model {
    /// <summary>
    /// 挂机信息
    /// 数据的存储方式是采用反射的方式
    /// </summary>
    public class KillMonsterData {
        /// <summary>
        /// 序列化文件名
        /// </summary>
        private string fileName;

        /// <summary>
        /// 技能信息
        /// </summary>
        public List<Skill> TheSkills = new List<Skill>();

        /// <summary>
        /// 挂机点信息
        /// </summary>
        public List<GuaJiDian> TheDians = new List<GuaJiDian>();

        /// <summary>
        /// 创建挂机信息对象
        /// </summary>
        /// <param name="fileName">可以用来反射创建对象的文件名</param>
        /// <returns></returns>
        public static KillMonsterData Create(string fileName) {
            KillMonsterData result;
            if (!Mrsoft.Core.XmlSaver<KillMonsterData>.FromXml(fileName, out result, typeof(XY), typeof(GuaJiDian), typeof(Skill))) {
                result = new KillMonsterData();
            }
            result.fileName = fileName;
            return result;
        }


        /// <summary>
        /// 获取地图中记录了的挂机点
        /// </summary>
        /// <param name="mapName">查询的地图</param>
        /// <param name="jsXy">查询时角色的坐标</param>
        /// <returns></returns>
        public GuaJiDian[] GetGuaDiansByMap(string mapName, XY jsXy) {
            return TheDians.Where(dian => dian.Map == mapName).OrderByDescending(dian => dian.GuaJiXy.HowFar(jsXy)).ToArray();
        }

        /// <summary>
        /// 将对象保存下来
        /// </summary>
        public bool Save() {
            return Mrsoft.Core.XmlSaver<KillMonsterData>.Save(fileName, this);
        }

        /// <summary>
        /// 获取可以释放的技能列表
        /// </summary>
        /// <returns></returns>
        public List<Skill> GetAvailableSkills() {
            return (from t in TheSkills where t.IsAnJian && (DateTime.Now - t.LastReleaseTime).TotalSeconds > t.CoolDown select t).ToList();
        }
    }
}